package cn.itcast.config;

import cn.itcast.tool.MyTool;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.client.advisor.vectorstore.QuestionAnswerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.ai.openai.OpenAiEmbeddingModel;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.SimpleVectorStore;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CommonConfige {
    /**
     * @param chatModel
     * @param chatMemory
     * @return
     */
    @Bean
    public ChatClient aliModel(OpenAiChatModel chatModel,
                               ChatMemory chatMemory,
                               MyTool myTool,
                               VectorStore vectorStore) {
        return ChatClient
                .builder(chatModel)
                .defaultSystem(SystemConstants.CUSTOMER_SERVICE_SYSTEM)
                //添加日志
                .defaultAdvisors(new SimpleLoggerAdvisor())
                //会话记忆
                .defaultAdvisors(MessageChatMemoryAdvisor.builder(chatMemory).build())
                .defaultAdvisors(QuestionAnswerAdvisor.builder(vectorStore)
                        //向量检索
                        .searchRequest(SearchRequest.builder()
                                //阈值
                                .similarityThreshold(0.6d)
                                //返回数量（默认为3）
                                .topK(3)
                                .build())
                        .build())
                .defaultTools(myTool)
                .build();
    }
    /**
     * 配置向量库
     * @param embeddingModel
     * @return
     */
    @Bean
    public VectorStore vectorStore(OpenAiEmbeddingModel embeddingModel) {
        return SimpleVectorStore.builder(embeddingModel).build();
    }
}